/*************** 刘灿  2018年04月22日 22时29分   系统管理 -- 用户设置    ********************************/
// 对应请求路径地址： cn.com.lsjz.NurseManager.controller.admin.UserController

//# sourceURL=user.js

var basePath = "admin/user/"; 

var deptList = null;

var roleList = null;

var options = "";

$(function(){
	
	$("#deptList").change(function(){
		var deptCode = $(this).val();
		queryUserList(deptCode);
	});
	
	loadDeptList();   // 初始化界面上的科室列表
	
	queryUserList();
	
	loadRoleList();
	
})

/*
 * 获取科室列表
 */
function loadDeptList(){
	$.ajax({
		type: 'POST',
		url: 'admin/dept/loadDeptList',
		dataType: 'json',
		data:{'deptName': null},
		success: function(result){
			deptList = result.data;
			options = "";
			for(var i=0;i<deptList.length;i++){
				var dept = deptList[i];
				options += '<option value="'+dept['deptCode']+'">'+dept['deptName']+'</option>'
			}
			$("#deptList").html(options);
			
			
			
		}
	});
}

function queryUserList(deptCode,userName){
	if(typeof(deptCode) == "undefined")
		deptCode = null;
	if(typeof(userName) == "undefined")
		userName = null;
	$.ajax({
		type: 'POST',
		url: basePath + 'loadUserList',
		dataType: 'json',
		data:{'deptCode': deptCode,'userName':userName},
		success: function(result){
			$('tbody').empty();
			var userList = result.data;
			if(userList != null && userList.length >0){
				var trs = "";
				for(var i=0;i<userList.length;i++){
					var user = userList[i];
					trs += '<tr class="text-c">'
						+ '<td><input type="checkbox" value="'+user['id']+'"></td>'
						+ '<td>'+user['id']+'</td>'
						+ '<td>'+user['userId']+'</td>'
						+ '<td>'+user['userName']+'</td>'
						+ '<td>'+user['sex']+'</td>'
						+ '<td>'+user['title']+'</td>'
						+ '<td>'+user['birthDate']+'</td>'
						+ '<td>'+user['phone']+'</td>'
						+ '<td>'+user['deptName']+'</td>'
						+ '<td class="td-status">'+(user.active == 1 ? '<span class="label label-success radius">已启用</span>' : '<span class="label label-success radius">已停用</span>')+'</td>'
						+ '<td class="td-manage">' +
						(user.active == 1 ? '<a style="text-decoration:none" onClick="user_active(this)" href="javascript:;" title="停用"><i class="Hui-iconfont">&#xe631;</i></a>'
								           : '<a style="text-decoration:none" onClick="user_active(this)" href="javascript:;" title="启用"><i class="Hui-iconfont">&#xe615;</i></a>')
								+'<a title="编辑用户" href="javascript:;" onclick="user_edit(this,'+user['id']+')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6df;</i></a>'
								+'<a title="权限分配" href="javascript:;" onclick="user_Permission(\''+user['userId']+'\')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6df;</i></a>'
								+'<a title="删除用户" href="javascript:;" onclick="user_del(this,'+user['id']+')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a>'
						+'</td>'
					+ '</tr>';
				}
				$('tbody').html(trs);
			}
		}
	});
}

/*
 * 查询所有的角色列表
 */
function loadRoleList(){
	
	$.ajax({
		type: 'POST',
		url: 'admin/role/loadRoleList',
		dataType: 'json',
		data:{'roleName': null},
		success: function(result){
			roleList = result.data;
		}
	});	
	
}


/* 新增弹窗 */
function user_add(){
	layer.open({
		type: 2,
		title: '新增用户',
		shadeClose: true,
	    shade: false,
	    maxmin: true, //开启最大化最小化按钮
		content: 'pages/admin/user/user-info.html',
		area: ['800px', '400px'], 
		success: function (layero, index) { // 层弹出后的成功回调方法  ----  layero 当前层dom、index 当前层索引
			// 获取弹出层上对应的控件对象
			var dom = layero.find("iframe")[0].contentWindow.document;
			$("#deptCode", dom).html(options); // 主键 
			$('#deptName', dom).val(deptList[0]['deptName']);
			$('input[name="sex"][value="女"]',dom).parent().addClass('checked');
		},
		end:function(){
			if($('#childFormMsg').val() == "保存成功"){ 
				queryUserList();
			}
		}
	});
}

/* 编辑弹窗 */
function user_edit(obj,id){
	layer.open({
		type: 2,
		title: '编辑角色',
		shadeClose: true,
	    shade: false,
	    maxmin: true, //开启最大化最小化按钮
		content: 'pages/admin/user/user-info.html',
		area: ['800px', '400px'], 
		success: function (layero, index) { // 层弹出后的成功回调方法  ----  layero 当前层dom、index 当前层索引
			// 获取弹出层上对应的控件对象
			var dom = layero.find("iframe")[0].contentWindow.document;
			$("#deptCode", dom).html(options); // 主键 
			$('#id',dom).val($(obj).parents('tr').children('td').eq(1).text());
			$("#userName", dom).val($(obj).parents('tr').children('td').eq(3).text());
			var sex = $(obj).parents('tr').children('td').eq(4).text();
			$('input[name="sex"][value="'+sex+'"]',dom).parent().addClass('checked');
			$("#title", dom).val($(obj).parents('tr').children('td').eq(5).text());
			$("#birthDate", dom).val($(obj).parents('tr').children('td').eq(6).text());
			$("#phone", dom).val($(obj).parents('tr').children('td').eq(7).text());
			var currDeptName = $(obj).parents('tr').children('td').eq(8).text();
			var depts = deptList.filter(function(item,index,array){
				return item["deptName"] == currDeptName;
			});
			if(depts.length > 0){
				$('#deptName', dom).val(depts[0]['deptName']);
				$('#deptCode', dom).val(depts[0]['deptCode']);
			}
		},
		end:function(){
			if($('#childFormMsg').val() == "保存成功"){
				queryUserList();
			}
		}
	});
}

/*
 * 删除
 */
function user_del(obj,id){
	layer.confirm('确认要删除吗？',function(index){
		$.ajax({
			type: 'DELETE',
			url: basePath + id,
			dataType: 'json',
			success: function(result){
				$(obj).parents("tr").remove();
				layer.msg(result.message,{icon:1,time:2000});
			},
			error:function(data) {
				console.log(data.message);
			},
		});		
	});
}

/*
 * 权限分配
 */
function user_Permission(userId){
	layer.open({
		type: 2,
		title: '编辑权限',
		shadeClose: true,
	    shade: false,
	    maxmin: true, //开启最大化最小化按钮
		content: 'pages/admin/user/user-permission-info.html',
		area: ['800px', '400px'], 
		success: function (layero, index) { // 层弹出后的成功回调方法  ----  layero 当前层dom、index 当前层索引
			// 获取弹出层上对应的控件对象
			var dom = layero.find("iframe")[0].contentWindow.document;
			// 角色处理
			var roles = "";
			for(var i=0;i<roleList.length;i++){
				roles += '<label class="ml-20"><input type="checkbox" value="'+roleList[i]['id']+'" name="role">'+roleList[i]['roleName']+'</label>';
			}
			$('#roleList',dom).html(roles);
			// 科室处理
			var depts = "";
			for(var i=0;i<deptList.length;i++){
				depts += '<label class="ml-20"><input type="checkbox" value="'+deptList[i]['deptCode']+'" name="dept">'+deptList[i]['deptName']+'</label>';
			}
			$('#deptList',dom).html(depts);
			$('#userId',dom).val(userId);
			
			$.ajax({
				type: 'get',
				url: "/admin/user/permission/" + userId,
				dataType: 'json',
				success: function(result){
					var roleList = result.data['roleList'];
					var deptList = result.data['deptList'];
					for(var i=0;i<roleList.length;i++){
						$('input[name="role"][value="'+roleList[i]['roleId']+'"]',dom).attr('checked',true);
					}
					for(var i=0;i<deptList.length;i++){
						$('input[name="dept"][value="'+deptList[i]['deptCode']+'"]',dom).attr('checked',true);
					}
				},
				error:function(data) {
					console.log(data.message);
				},
			});	
			
		}
	});
}

/*
 * 状态更新
 */
function user_active(obj){
	var id =$(obj).parents("tr").children('td').eq(1).text(); 
	var status = $(obj).attr('title') == '启用' ? 1 : 0;
	var msg = status == 0 ? '确定要停用此用户？' : '确定要启用此用户？';
	layer.confirm(msg, function(){
		$.ajax({
			type: 'post',
			url: basePath + "active",
			dataType: 'json',
			data:{'id':id,'active':status},
			success: function(result){
				$(obj).parent("td").prev('td').html(status==0?'<span class="label label-warning radius">已停用</span>':'<span class="label label-success radius">已启用</span>');
				$(obj).attr('title',status == 0 ? '启用' : '停用');
				$(obj).find('.Hui-iconfont').html(status == 0 ? '&#xe615;' : '&#xe631;');
				layer.msg(result.message,{icon:1,time:2000});
			},
			error:function(data) {
				console.log(data.message);
			},
		});	
	});
}

